$( document ).ready(function() {
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("[name='appointDocId']").val("");
	$("[name='appointDocFirstname']").val("");
	$("[name='appointDocLastname']").val("");
	$("[name='recordDocId']").val("");
	$("[name='recordPatientId']").val("");
	/*
	$("[name='yearStart']").val("-1");
	$("[name='monthStart']").val("-1");
	$("[name='dayStart']").val("-1");
	$("[name='yearEnd']").val("-1");
	$("[name='monthEnd']").val("-1");
	$("[name='dayEnd']").val("-1");
	*/
	$("#chooseStartDate").datepicker();
	$("#chooseEndDate").datepicker();
	$("#chooseStartDate").val("");
	$("#chooseStartTime").val("");
	$("#chooseEndDate").val("");
	$("#chooseEndTime").val("");
});

function getDocSearchResult(){
	$("#appointDocSearchResult").empty();
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("#recordDetail").empty();
	$.getJSON("financial/docSearch.html?appointDocId="+$("[name='appointDocId']").val()+"&appointDocFirstname="+$("[name='appointDocFirstname']").val()+"&appointDocLastname="+$("[name='appointDocLastname']").val(), function(data) {
		if(!jQuery.isEmptyObject(data.appointDocSearchResult)){
			$("#appointDocSearchResult").data("docSearchResult", data);
			printDocTableBody(data);
			$("#appointDocSearchResult").append("<br /><button id=\"confirmDoc\" onclick=\"confirmDoc()\" class=\"button\">confirm</button>&nbsp;");
		}else{
			$("#appointDocSearchResult").append("<strong>No result.</strong>");
		}
		
	});
}

function clearDtable(){
	$("#appointDocSearchResult").empty();
}

function printDocTableBody(data){
	$("#appointDocSearchResult")
	.append(
			"<table id=\"docList\" class=\"tablecss\">" +
			"<thead>" +
			"<tr>" +
			"<th></th>"+
			"<th>Doctor id</th>" +
			"<th>First name</th>" +
			"<th>Last name</th>" +
			"<th>Gender</th>" +
			"<th>Office phone number</th>" +
			"<th>Office room</th>" +
			"<th>Department</th>" +
			"</tr>" +
			"</thead>" +
			"<tbody id=\"docTableBody\">" +
			"</tbody>" +
			"</table>");
var tr;
$(data).each(
	function(index, value) {
		$(value.appointDocSearchResult).each(function (i,v){
			tr = $("<tr>");
			tr.append("<td><input type=\"radio\" name=\"docRadio\" value=\""+v.userId+"\"></td>");
			tr.append("<td>" + v.userId + "</td>");
			tr.append("<td>" + v.firstname + "</td>");
			tr.append("<td>" + v.lastname + "</td>");
			var gen=v.gender;
			if(gen=='m'){
				tr.append("<td>male</td>");
			}else if(gen=='f'){
				tr.append("<td>female</td>");
			}else{
				tr.append("<td></td>");
			}
			tr.append("<td>" +v.officeInfo.officePhone+"</td>");
			tr.append("<td>" +v.officeInfo.officeDept.officeRoom+"</td>");
			tr.append("<td>" +v.officeInfo.officeDept.deptInfo.deptName+"</td>");
			tr.append("</tr>");
			$("#docTableBody").append(tr);
		});
		
	});
$("#docList").tablesorter({
	widgets : [ 'zebra' ],
	headers : {
		0 : {
			sorter : false
		}
	}
});
}

function confirmDoc(){
	var id=$("[name=\"docRadio\"]:checked").val();
	$("[name=\"recordDocId\"]").val(id);
	$("#appointDocSearchResult").empty();
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("#recordDetail").empty();
	$.getJSON("financial/recordList.html?recordListDocId="+id, function(data){
		if(!jQuery.isEmptyObject(data.recordDetailList)){
			$("#recordDetail").data("recordDetails", data);
			printRecordTableBody(data);
			$("#recordDetail").append("<strong>Total: "+data.recordDetailList.length+"</strong>");
		}else{
			$("#recordDetail").append("<strong>No result.</strong>");
		}
	});
}
function printRecordTableBody(data) {
	
	$("#recordDetail")
			.append(
					"<table id=\"list\" class=\"tablecss\">" +
					"<thead>" +
					"<tr>" +
					"<th>Record id</th>"+
					"<th>Patient id</th>" +
					"<th>Doctor id</th>" +
					"<th>Doctor name</th>" +
					"<th>Start time</th>" +
					"<th>End time</th>" +
					"<th>Diagnosis</th>" +
					"<th>Treatment Schedule</th>" +
					"<th>Drug</th>" +
					"</tr>" +
					"</thead>" +
					"<tbody id=\"tableBody\">" +
					"</tbody>" +
					"</table>");
	var tr;
	$(data).each(
			function(index, value) {
				$(value.recordDetailList).each(function (i,v){
					tr = $("<tr>");
					tr.append("<td>" + v.recordId + "</td>");
					tr.append("<td>" + v.patientId + "</td>");
					tr.append("<td>" + v.doctorId + "</td>");
					tr.append("<td>" + v.doctorName + "</td>");
					tr.append("<td>" + convertDateTime(v.startTime) + "</td>");
					tr.append("<td>" + convertDateTime(v.endTime) + "</td>");
					tr.append("<td>" + v.diagnosis + "</td>");
					tr.append("<td>" + v.treatmentSchedule + "</td>");
					tr.append("<td>" + v.drug + "</td>");
					tr.append("</tr>");
					$("#tableBody").append(tr);
				});
				
			});
	$("#list").tablesorter({
		widgets : [ 'zebra' ]
	});
}

function convertDateTime(milliSec){
	var dateTime=new Date(milliSec);
	var format=new String("");
	format=dateTime.getFullYear()+"-";
	if((dateTime.getMonth()+1)<10){
		format=format+"0"+(dateTime.getMonth()+1)+"-";
	}else{
		format=format+(dateTime.getMonth()+1)+"-";
	}
	if(dateTime.getDate()<10){
		format=format+"0"+dateTime.getDate()+" ";
	}else{
		format=format+dateTime.getDate()+" ";
	}
	if(dateTime.getHours()<10){
		format=format+"0"+dateTime.getHours()+":";
	}else{
		format=format+dateTime.getHours()+":";
	}
	if(dateTime.getMinutes()<10){
		format=format+"0"+dateTime.getMinutes();
	}else{
		format=format+dateTime.getMinutes();
	}
	return format;
}

function getRecordSearchResult(){
	$("#appointDocSearchResult").empty();
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("#recordDetail").empty();
	$.getJSON("financial/searchRecord.html?recordDocId="+$("[name='recordDocId']").val()+"&recordPatientId="+$("[name='recordPatientId']").val()+"&startDate="+$("#chooseStartDate").val()+"&endDate="+$("#chooseEndDate").val(), function(data){
		if(data.flag==false){
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("error");
			$("#messageDiv").html("<strong>"+data.message+"</strong>");
			$("#messageDiv").show();
		}else if(data.flag==true){
			if(!jQuery.isEmptyObject(data.recordDetailList)){
				$("#recordDetail").data("recordDetails", data);
				printRecordTableBody(data);
				$("#recordDetail").append("<strong>Total: "+data.recordDetailList.length+"</strong>");
			}else{
				$("#recordDetail").append("<strong>No result.</strong>");
			}
		}
	});
}